<template>
  <div class="table-wrap">
    <div class="search_header">
      <span>
        <el-button
          icon="el-icon-plus"
          @click="addProject"
          v-has="'projectManagement:add'"
          >新增项目</el-button
        >
      </span>
      <div class="search_header_right">
        <el-select
          placeholder="项目类型"
          v-model="parameter.projectType"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in projectTypeOptions"
            :key="item.dictValue"
            :label="item.dictText"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        <el-input
          placeholder="请输入平台名称"
          v-model="parameter.keyWord"
          @keydown.enter.native="search"
          clearable
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
      </div>
    </div>
    <el-table
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB ',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
      v-loading="loading"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="平台名称"
        prop="platformName"
        show-overflow-tooltip
      />
      <el-table-column
        label="责任单位"
        prop="zrUnit"
        show-overflow-tooltip
      />
      <el-table-column
        label="投资额（万元）"
        prop="investmentAmount"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目类型"
        prop="otherProjectType"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.otherProjectType === 'msxd'"
            >岷山行动</span
          >
          <span v-if="scope.row.otherProjectType === 'zsfx'"
            >中试放心</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="上会时间"
        prop="shTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="签约时间"
        prop="qyTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="联系人"
        prop="contactPerson"
        show-overflow-tooltip
      />
      <el-table-column
        label="联系方式"
        prop="contactInformation"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        label="更新时间"
        prop="updateTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            @click="updateProgress(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            v-has="'projectManagement:edit'"
            >进度更新</el-button
          >
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-has="'projectManagement:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  queryProjectList,
  deleteProjectInfo
} from '@/api/innovation'
import { getDictOptions } from '@/utils/dict'
import Icon from '@/components/icon'

export default {
  name: 'index',
  components: {
    Icon
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        projectType: '',
        keyWord: '',
        pageIndex: 1,
        pageSize: 10
      },
      projectTypeOptions: [],
      loading: false
    }
  },
  mounted() {
    this.queryTypeOptions()
    this.getList()
  },
  methods: {
    // 项目分类
    async queryTypeOptions() {
      this.projectTypeOptions = await getDictOptions(
        'zhkc_xmgl_xmlx'
      )
    },

    search() {
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    // 获取用户列表
    getList() {
      this.loading = true
      queryProjectList(this.parameter).then((res) => {
        if (res.success) {
          this.total = res.data.count
          this.projectList = res.data.list
        }
        this.loading = false
      })
    },

    // 表格高亮
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    addProject() {
      this.$router.push({
        path: '/wisdomInnovation/projectManagement/add',
        query: {
          title: '新增'
        }
      })
    },

    // 进度更新
    updateProgress(row) {
      // this.$emit('updateProgress', row)
      this.$router.push({
        path: '/wisdomInnovation/projectManagement/add',
        query: {
          pkid: row.pkid,
          title: '进度更新',
          isEdit: true
        }
      })
    },
    // 项目查看
    viewProject(row) {
      this.$router.push({
        path: '/wisdomInnovation/projectManagement/add',
        query: {
          pkid: row.pkid,
          isView: false,
          isCheck: false,
          title: '查看',
          isEdit: true,
          isDetail: true
        }
      })
    },

    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该项目，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteProjectInfo(row.pkid).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              if (
                this.total % 10 === 1 &&
                this.parameter.pageIndex ===
                  parseInt(this.total / 10) + 1 &&
                this.parameter.pageIndex !== 1
              ) {
                this.parameter.pageIndex -= 1
              }
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
